<template>
  <div class="search-history">
    <van-cell title="历史记录">
      <div v-if="isDeleteShow">
        <span @click="$emit('clear-search-histories')" style="margin-right:10px"
          >全部删除</span
        >
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true"></van-icon>
    </van-cell>
    <van-cell
      :title="item"
      v-for="(item, index) in SearchHistories"
      :key="index"
      @click="onSearchItemClick(item, index)"
    >
      <van-icon v-if="isDeleteShow" name="close"></van-icon>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: "SearchHistory",
  components: {},
  props: {
    SearchHistories: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      // 定义当前是否是删除状态
      isDeleteShow: false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 删除状态，删除历史记录数据
        this.SearchHistories.splice(index, 1);
      } else {
        // 删除状态，直接进入搜索
        this.$emit("search", item);
      }
    },
  },
};
</script>

<style scoped lang="less">
.search-history {
  .van-cell__title,
  .van-cell__value {
    margin-top: 10px;
  }
}
</style>
